package com.bjshiwei.zhengu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.bean.RankingBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：Administrator on 2016/10/10 0010 13:16
 * 邮箱：sanjins@126.com
 */


public class RankingLVAdapter extends MyBaseAdapter<RankingBean>{
    private List<RankingBean> mData;
    private Context mContext;

    //标示是否已经浏览过
    public static Map<Integer, Boolean> isSelected;
    // 用来保存之前选中状态的位置，用于下拉刷新和上拉加载更多数据时恢复已选中的位置
    public static List<Integer> hasSelected = new ArrayList<>();

    /**
     * 初始选中状态
     *
     * @param size 表示数据的长度，是为了解决下拉刷新和上拉加载时产生新的item时能够都有默认初始值
     */
    private void initSelected(int size) {
        //判断isSelected是否已经存在
        if (isSelected == null) {
            isSelected = new HashMap<>();
            for (int i = 0; i < size; i++) {
                isSelected.put(i, false);
            }
        }
    }

    public RankingLVAdapter(Context context, List<RankingBean> datas) {
        super(context, datas);
        mData =datas;
        mContext =context;
        initSelected(mData==null?0:mData.size());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if(convertView==null) {
            convertView = View.inflate(mContext, R.layout.item_market,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(!TextUtils.isEmpty(mData.get(position).getTrade())) {
            viewHolder.mTextViewTrade.setText(mData.get(position).getTrade());
        }

        if(!TextUtils.isEmpty(mData.get(position).getTradeRate())) {
            if(!mData.get(position).getTradeRate().contains("-")) {
                viewHolder.mTextViewTradeRate.setTextColor(mContext.getResources().getColor(R.color.stockRed));
                viewHolder.mTextViewName.setTextColor(mContext.getResources().getColor(R.color.stockRed));
                viewHolder.mTextViewChangeRate.setTextColor(mContext.getResources().getColor(R.color.stockRed));
                viewHolder.mTextViewTradeRate.setText("+"+mData.get(position).getTradeRate());
            }else{
                viewHolder.mTextViewTradeRate.setTextColor(mContext.getResources().getColor(R.color.green));
                viewHolder.mTextViewName.setTextColor(mContext.getResources().getColor(R.color.green));
                viewHolder.mTextViewChangeRate.setTextColor(mContext.getResources().getColor(R.color.green));
                viewHolder.mTextViewTradeRate.setText(mData.get(position).getTradeRate());
            }

        }



        if(!TextUtils.isEmpty(mData.get(position).getName())) {
            viewHolder.mTextViewName.setText(mData.get(position).getName());
        }

        if(!TextUtils.isEmpty(mData.get(position).getChangeRate())) {

            if(!mData.get(position).getChangeRate().contains("-")) {
                viewHolder.mTextViewChangeRate.setText(mData.get(position).getChangeRate());

            }else{
                viewHolder.mTextViewChangeRate.setText(mData.get(position).getChangeRate());
            }
        }

        return convertView;
    }

    class ViewHolder{
        private TextView mTextViewTrade,mTextViewTradeRate,mTextViewName,mTextViewChangeRate;
        public ViewHolder(View view){
            mTextViewTrade = (TextView) view.findViewById(R.id.tv_market_trade);
            mTextViewTradeRate = (TextView) view.findViewById(R.id.tv_market_traderate);
            mTextViewName = (TextView) view.findViewById(R.id.tv_market_name);
            mTextViewChangeRate = (TextView) view.findViewById(R.id.tv_market_changerate);
        }
    }
}
